package com.cloudcc.ccTool;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.*;

public class UserInfo extends CCObject{
	public String binding;
	ResourceBundle prb = PropertyResourceBundle.getBundle("application");// 获取配置文件;

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}
	public String orgId;

	public String getGetLoginName() {
		return prb.getString("username");
	}

	public void setGetLoginName(String getLoginName) {
		this.getLoginName = getLoginName;
	}

	public String language;

	public String getLanguage() {
		return this.language;
	}

	public String getLoginName;
	public String getOrgId() {
		return orgId;
	}
	public String getRoleId() {
		return  (String)this.get("role");
	}

	public UserInfo (String username,String pwd) throws Exception {
		this.put("userName", username);
		this.put("password", pwd);
	}
	public String getUserId() {
		CCService cs = new CCService(new UserInfo());
//		System.out.println("select id from tp_sys_user where login_name = '"+prb.getString("username")+"'");
		if("".equals(this.getUserName())){
			this.put("userName",prb.getString("username"));
		}
		List<CCObject> ccObjectList = cs.cqlQuery("tp_sys_user","select id from tp_sys_user where login_name = '"+this.getUserName()+"'");
		return ccObjectList.get(0).get("id")+"";
	}


	public String getTimeZone() {
		CCService cs = new CCService(new UserInfo());
//		System.out.println("select id from tp_sys_user where login_name = '"+prb.getString("username")+"'");
		if("".equals(this.getUserName())){
			this.put("userName",prb.getString("username"));
		}
		List<CCObject> ccObjectList = cs.cqlQuery("tp_sys_user","select usertimezone from tp_sys_user where login_name = '"+this.getUserName()+"'");
		String userTimeZone = "";
		if(ccObjectList.size()>0){
			userTimeZone = StringUtil.str(ccObjectList.get(0).get("usertimezone"));
		}
		return timeZoneMap(userTimeZone);
	}

	public UserInfo() {
		this.setOrgId(prb.getString("orgId"));
	}

	public String getLoginName(){
		return this.get("userName")==null?"":(String)this.get("userName");
	}
	public String getProfileId() {
		CCService cs = new CCService(new UserInfo());

		if("".equals(this.getUserName())){
			this.put("userName",prb.getString("username"));
		}
		List<CCObject> ccObjectList = cs.cqlQuery("tp_sys_user","select profile_id from tp_sys_user where login_name = '"+this.getUserName()+"'");
		return ccObjectList.get(0).get("profile_id")+"";
	}
	public String getProfileName() {
		 return "";
	}


	public void setBinding(String bind){
		this.binding = bind;
		this.put("binding",bind);
	}
	public String getBinding(){
		if (this.binding == null){
			return (String)this.get("binding");
		} else {
			return this.binding;
		}
	}
	public String getUserName() {
//		return  (String)this.get("name");
		return  prb.getString("username");
	}

	/**
	 * 使用md5的算法进行加密
	 */
	public static String md5(String plainText) {
		byte[] secretBytes = null;
		try {
			secretBytes = MessageDigest.getInstance("md5").digest(
					plainText.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("没有md5这个算法！");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字
		// 如果生成数字未满32位，需要前面补0
		for (int i = 0; i < 32 - md5code.length(); i++) {
			md5code = "0" + md5code;
		}
		return md5code;
	}


	public static String timeZoneMap(String lable){
		Map<String,String> timezone = new HashMap<String,String>();
		timezone.put("G1","Pacific/Kiritimati");
		timezone.put("G2","Pacific/Chatham");
		timezone.put("G3","Pacific/Auckland");
		timezone.put("G4","Pacific/Enderbury");
		timezone.put("G5","Pacific/Tongatapu");
		timezone.put("G6","Asia/Kamchatka");
		timezone.put("G7","Pacific/Fiji");
		timezone.put("G8","Pacific/Norfolk");
		timezone.put("G9","Australia/Lord_Howe");
		timezone.put("G10","Australia/Sydney");
		timezone.put("G11","Pacific/Guadalcanal");
		timezone.put("G12","Australia/Adelaide");
		timezone.put("G13","Australia/Brisbane");
		timezone.put("G14","Australia/Darwin");
		timezone.put("G15","Asia/Seoul");
		timezone.put("G16","Asia/Tokyo");
		timezone.put("G17","Asia/Hong_Kong");
		timezone.put("G18","Asia/Kuala_Lumpur");
		timezone.put("G19","Asia/Manila");
		timezone.put("G20","Asia/Shanghai");
		timezone.put("G21","Asia/Singapore");
		timezone.put("G22","Asia/Taipei");
		timezone.put("G23","Australia/Perth");
		timezone.put("G24","Asia/Bangkok");
		timezone.put("G25","Asia/Jakarta");
		timezone.put("G26","Asia/Saigon");
		timezone.put("G27","Asia/Rangoon");
		timezone.put("G28","Asia/Dacca");
		timezone.put("G29","Asia/Yekaterinburg");
		timezone.put("G30","Asia/Katmandu");
		timezone.put("G31","Asia/Calcutta");
		timezone.put("G32","Asia/Colombo");
		timezone.put("G33","Asia/Karachi");
		timezone.put("G34","Asia/Tashkent");
		timezone.put("G35","Asia/Kabul");
		timezone.put("G36","Asia/Dubai");
		timezone.put("G37","Asia/Tbilisi");
		timezone.put("G38","Europe/Moscow");
		timezone.put("G39","Asia/Tehran");
		timezone.put("G40","Africa/Nairobi");
		timezone.put("G41","Asia/Baghdad");
		timezone.put("G42","Asia/Kuwait");
		timezone.put("G43","Asia/Riyadh");
		timezone.put("G44","Europe/Minsk");
		timezone.put("G45","Africa/Cairo");
		timezone.put("G46","Africa/Johannesburg");
		timezone.put("G47","Asia/Jerusalem");
		timezone.put("G48","Europe/Athens");
		timezone.put("G49","Europe/Bucharest");
		timezone.put("G50","Europe/Helsinki");
		timezone.put("G51","Europe/Istanbul");
		timezone.put("G52","Africa/Algiers");
		timezone.put("G53","Europe/Amsterdam");
		timezone.put("G54","Europe/Berlin");
		timezone.put("G55","Europe/Brussels");
		timezone.put("G56","Europe/Paris");
		timezone.put("G57","Europe/Prague");
		timezone.put("G58","Europe/Rome");
		timezone.put("G59","Europe/Dublin");
		timezone.put("G60","Europe/Lisbon");
		timezone.put("G61","Europe/London");
		timezone.put("G62","GMT");
		timezone.put("G63","Atlantic/Cape_Verde");
		timezone.put("G64","Atlantic/South_Georgia");
		timezone.put("G65","America/St_Johns");
		timezone.put("G66","America/Buenos_Aires");
		timezone.put("G67","America/Halifax");
		timezone.put("G68","America/Sao_Paulo");
		timezone.put("G69","Atlantic/Bermuda");
		timezone.put("G70","America/Indianapolis");
		timezone.put("G71","America/New_York");
		timezone.put("G72","America/Puerto_Rico");
		timezone.put("G73","America/Santiago");
		timezone.put("G74","America/Caracas");
		timezone.put("G75","America/Bogota");
		timezone.put("G76","America/Chicago");
		timezone.put("G77","America/Lima");
		timezone.put("G78","America/Panama");
		timezone.put("G79","America/Denver");
		timezone.put("G80","America/El_Salvador");
		timezone.put("G81","America/Mexico_City");
		timezone.put("G82","America/Los_Angeles");
		timezone.put("G83","America/Phoenix");
		timezone.put("G84","America/Tijuana");
		timezone.put("G85","America/Anchorage");
		timezone.put("G86","Pacific/Honolulu");
		timezone.put("G87","Pacific/Niue");
		timezone.put("G88","Pacific/Pago_Pago");
		timezone.put("G89","Asia/Beirut");
		timezone.put("G90","Europe/Belgrade");
		timezone.put("G91","America/Vancouver");
		timezone.put("G92","America/Maceio");

		return timezone.get(lable);
	}

	public String getEmail() {
		return  "";
	}


}

